var dataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/kpiDefine/queryKpiDefine",
		columns : [
				{
					data : "kpi_id",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "kpi_id"
				},{
					"data" : "code"
				}, {
					"data" : "kpi_name"
				}, {
					"data" : "device_type_name"
				}, {
					"data" : "device_name"
				},{
					"data": "is_use_comment",
				},{
					"data": "value_type",
				},{
					"data": "obj_type_comment",
				},{
					"data": "device_type_id",
				},{
					"data": "obj_id",
				},{
					"data": "is_use",
				},{
					"data": "obj_type",
				},{
					"data": "bit"
				}],
		columnDefs: [    
		        {    
		        	"targets": [9,10,11,12,13],    
		        	"visible": false    
		        }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};

//初始化表格
var table = $('#kpi_table').DataTable(dataTableConfig);

//全选
$(function() {
	//全选
	$("#checkall_kpi_define").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
		function() {
			var $tmp = $('[name=items]:checkbox');
			$('#checkall_kpi_define').prop('checked',
					$tmp.length == $tmp.filter(':checked').length);
		}	
	);
});

//树设置
var setting = {
		async: {
			enable: true,
			url:"/kpiDefine/getTreeData",
			autoParam:["id", "name"],
			dataFilter: filter
		},
		callback: {
			onClick:nodeOnClick
		}
};

var filter = function filter(treeId, parentNode, childNodes) {
	if (!childNodes) return null;
	for (var i=0, l=childNodes.length; i<l; i++) {
		childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
	}
	return childNodes;
};


function nodeOnClick(event, treeId, treeNode, clickFlag) {
	var url = '/kpiDefine/queryKpiDefine';
	var data = {};
	var tid =treeNode.id;
	if(tid.indexOf('type') != -1){
		data.tree_type_id = tid.substring(0, tid.indexOf('_type'));
		$('#kpi_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_kpi_define').removeAttr("disabled");
		};
		table=$('#kpi_table').dataTable(dataTableConfig);
		document.getElementById("type_id_kpi_define").options[0].selected=true;
		document.getElementById("type_id_kpi_define").options[0].value = tid.substring(0, tid.indexOf('_type'));;
		document.getElementById("type_id_kpi_define").options[0].text= treeNode.name;
	}
	
};		


var app = angular.module('kpiDefineApp', []);

var initController = app.controller("kpiDefineController" , function($scope, $http){
	$('#addModal_kpi_define').on('hide.bs.modal', function () {
		document.getElementById("type_id_kpi_define").options[0].selected=true;
		document.getElementById("type_id_kpi_define").options[0].value = "";
		document.getElementById("type_id_kpi_define").options[0].text= "";
		document.getElementById("addForm_kpi_define").reset(); 
	});
	
	$.fn.zTree.init($("#tree_kpi_define"), setting);
	$http({
		url : '/deviceManager/getDeviceTypeList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.deviceTypeList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$http({
		url : '/deviceManager/getDeviceList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.deviceList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$scope.add = function(){
		var form = document.getElementById("addForm_kpi_define");
		var code = form.code_kpi_define.value;
		var name = form.name_kpi_define.value;
		var type_id = form.type_id_kpi_define.value;
		var obj_id = form.obj_id_kpi_define.value;
		var is_use = form.is_use_kpi_define.value;
		var value_type = form.value_type_kpi_define.value;
		var bit = form.bit_kpi_define.value;
		var obj_type = form.obj_type_kpi_define.value;
		$http({
			url : '/kpiDefine/add',
			params:{code:code, name:name, type_id:type_id , obj_id:obj_id, is_use:is_use,
				value_type:value_type, bit:bit, obj_type:obj_type},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#addSuccessAlert_kpi_define').fadeIn(2000);
				$('#addSuccessAlert_kpi_define').fadeOut(2000);
				table.draw();
			}else{
				$('#addFailedAlert_kpi_define').fadeIn(2000);
				$('#addFailedAlert_kpi_define').fadeOut(2000);
			}
		}).error(function(data, header, config, status) {
			// 处理响应失败
			$('#addFailedAlert_kpi_define').fadeIn(2000);
			$('#addFailedAlert_kpi_define').fadeOut(2000);
		});
	};
	$scope.del_init=function(){
		var kpiIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			kpiIds += $(this).val() + ";";
		});
		if(kpiIds==""){
			$('#selAlert_kpi_define').html("<strong>请选择记录进行删除！</strong>");
			$('#alertModal_kpi_define').modal("toggle");
		}else{
			$("#delModal_kpi_define").modal("toggle");
		}
		
	};
	$scope.del = function(){
		var kpiIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			kpiIds += $(this).val() + ";";
		});
		if(kpiIds==""){
			alert("请选择要删除的记录！");
		}else{
			$http({
				url : '/kpiDefine/delete',
				params:{kpiIds:kpiIds},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data==true){
					$('#promptContent_kpi_define').hide();
					$('#del_kpi_define').hide();
					$('#deleteSuccessAlert_kpi_define').fadeIn(2000);
					$('#delModal_kpi_define').on('hide.bs.modal', function () {
						table.draw();
						$('#del_kpi_define').show();
						$('#deleteSuccessAlert_kpi_define').hide();
						$('#promptContent_kpi_define').show();
					});
				}else{
					$('#promptContent_kpi_define').hide();
					$('#del_kpi_define').hide();
					$('#deleteFailedAlert_kpi_define').fadeIn(2000);
					$('#delModal_kpi_define').on('hide.bs.modal', function () {
						table.draw();
						$('#del_kpi_define').show();
						$('#deleteFailedAlert_kpi_define').hide();
						$('#promptContent_kpi_define').show();
					});
				}
				
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#promptContent_kpi_define').hide();
				$('#del_kpi_define').hide();
				$('#deleteFailedAlert_kpi_define').fadeIn(2000);
				$('#delModal_kpi_define').on('hide.bs.modal', function () {
					$('#del_kpi_define').show();
					$('#deleteFailedAlert_kpi_define').hide();
					$('#promptContent_kpi_define').show();
				});
			});
		}
	};
	$scope.update_init =function(){
		var kpiIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			kpiIds += $(this).val() + ";";
		});
		if(kpiIds==""){
			$('#selAlert_kpi_define').html("<strong>请选择要修改的记录！</strong>");
			$('#alertModal_kpi_define').modal("toggle");
		}else if(kpiIds.split(";").length>2){
			$('#selAlert_kpi_define').html("<strong>修改的记录数不能大于1.</strong>");
			$('#alertModal_kpi_define').modal("toggle");
		}else{
			  
			 $("table :checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var selRow =$('#kpi_table').dataTable().fnGetData(key-1);
		            	$scope.kpi_id_update = selRow.kpi_id;
		            	$scope.code_update = selRow.code;
		            	$scope.name_update = selRow.kpi_name;
		            	$scope.type_id_update = selRow.device_type_id;
		            	$scope.device_type_name_update = selRow.device_type_name;
		            	$scope.obj_id_update = selRow.obj_id;
		            	$scope.device_name_update = selRow.device_name;
		            	$scope.is_use_update = selRow.is_use;
		            	$scope.value_type_update = selRow.value_type;
		            	$scope.bit_update = selRow.bit;
		            	$scope.obj_type_update = selRow.obj_type;
		            	
		            }
			 });
			 $("#updateModal_kpi_define").modal("toggle");
		}
	};
	$scope.update = function(){
		var form = document.getElementById("updateForm_kpi_define");
		var kpi_id = form.kpi_id_update_kpi_define.value;
		var code = form.code_update_kpi_define.value;
		var name = form.name_update_kpi_define.value;
		var type_id = form.type_id_update_kpi_define.value;
		var obj_id = form.obj_id_update_kpi_define.value;
		var is_use = form.is_use_update_kpi_define.value;
		var value_type = form.value_type_update_kpi_define.value;
		var bit = form.bit_update_kpi_define.value;
		var obj_type = form.obj_type_update_kpi_define.value;
		$http({
			url : '/kpiDefine/edit',
			params:{kpi_id:kpi_id, code:code, name:name, type_id:type_id , 
				obj_id:obj_id, is_use:is_use, value_type:value_type, bit:bit, obj_type:obj_type},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#updateSuccessAlert_kpi_define').fadeIn(2000);
				$('#updateSuccessAlert_kpi_define').fadeOut(2000);
				table.draw();
			}else{
				$('#updateFailedAlert_kpi_define').fadeIn(2000);
				$('#updateFailedAlert_kpi_define').fadeOut(2000);
			}
		}).error(function(data, header, config, status){
			// 处理响应失败
			$('#updateFailedAlert_kpi_define').fadeIn(2000);
			$('#updateFailedAlert_kpi_define').fadeOut(2000);
		});
	}
	
	$scope.query = function () {
		var url = '/kpiDefine/queryKpiDefine';
		var data = {};
		var form =document.getElementById("queryForm_kpi_define");  // $('#queryForm');
		if(form.code_query_kpi_define.value!=0){
			data.code =  form.code_query_kpi_define.value;
		}
		if(form.name_query_kpi_define.value!=0){
			data.name = form.name_query_kpi_define.value;
		}
		if(form.type_id_query_kpi_define.value!=0){
			data.type_id=  form.type_id_query_kpi_define.value;
		}
		if(form.is_use_query_kpi_define.value!=0){
			data.is_use =  form.is_use_query_kpi_define.value;
		}
		if(form.obj_type_query_kpi_define.value!=0){
			data.obj_type =  form.obj_type_query_kpi_define.value;
		}
		$('#queryBtn_kpi_define').attr("disabled", "true");
		$('#kpi_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_kpi_define').removeAttr("disabled");
		};
		table=$('#kpi_table').dataTable(dataTableConfig);
	};
	
});